﻿<?php
	require_once("../class/mysql_help.class.php");
	require_once("../config/admin_config.php");
	class MessageBoard
	{
		var $sqlHelper;
		
		/*构造函数*/
		public function __construct()
		{
			$this->sqlHelper = new db_Mysql();
		}
		
		/*功能：添加留言
		@ $id_employMessage 招聘信息的id
		@ $id_role  留言人的id
		@ $type_role  留言人的角色(毕业生或者企业)
		@ $content  留言的内容
		*/
		public function AddMessage($id_employMessage,$id_role,$type_role,$content)
		{
			$num_reply = 0;//新增留言回复数默认为0
			$date_public = time();
			//获取留言人的名字
			$cmd = null;
			if($type_role == 1)//毕业生
			{
				$cmd = "Select name From graduate Where id = '".$id_role."'";
			}
			else if($type_role == 2)//企业
			{
				$cmd = "Select companyName as name From company Where id = '".$id_role."'";
			}
			//查询数据库获取名字
			$this->sqlHelper->dbconnect();
			$result = $this->sqlHelper->execute($cmd);
			$row = $this->sqlHelper->fetch_array($result);
			$this->sqlHelper->free_result($result);
			$this->sqlHelper->dbclose();
			$cmd = "Insert Into message_board(id_employ_message,id_role,name,type_role,content,num_reply,date_public) values ('".
				$id_employMessage."','".$id_role."','".$row["name"]."','".$type_role."','".$content."','".$num_reply."','".$date_public."')";
			$this->sqlHelper->dbconnect();
			$id_added = $this->sqlHelper->insert($cmd);//获取插入数据的id
			$this->sqlHelper->dbclose();
			return $id_added;//返回新插入数据的id
		}
			
		/*功能：删除留言
		@ $id_message  要删除的留言信息的id
		*/
		public function DeleteMessage($id_message)
		{
			$cmd = "delete * from message_board where id = '".$id_message."'";
			$this->sqlHelper->dbconnect();
			$num_affected = $this->sqlHelper->delete($id_message);//获取受影响的记录数
			$this->sqlHelper->dbclose();
			return $num_affrected;//返回受影响的记录数
		}

		/*功能：添加一条回复后，更新回复记录
		@ $id_comment 对应留言的id
		//返回：返回受影响的记录数(1为成功，0为失败)
		*/
		public function AddReplyNum($id_comment)
		{
			$cmd = "Update message_board Set num_reply = num_reply + 1 Where id = '".$id_comment."'";
			$this->sqlHelper->dbconnect();
			$num_affected = $this->sqlHelper->update($cmd);
			$this->sqlHelper->dbclose();
			return $num_affected;
		}
		
		/*功能：根据id查询一条留言记录（主要用来显示新增的评论）
		@ $id_comment  评论的id
		//返回：数组类型 $row;
		*/
		public function SelectOneById($id_comment)
		{
			$cmd = "Select * From message_board Where id = '".$id_comment."'";
			$this->sqlHelper->dbconnect();
			$result = $this->sqlHelper->execute($cmd);
			$this->sqlHelper->dbclose();
			$row = $this->sqlHelper->fetch_array($result);
			$this->sqlHelper->free_result($result);
			return $row;
		}
		
		/*功能：查询留言记录
		@ $id_employMessage  招聘信息的id
		@ $num_page  留言页码
		//返回：select出来的$result;
		//注意：使用完返回的$result后要记得free哦！
		*/
		public function SelectAllByEmployMessageId($id_employMessage,$num_page)
		{
			global $MessageBoard_Page_Size;
			//echo '<script>alert("'.$id_employMessage.','.$num_page.','.$MessageBoard_Page_Size.'");</script>';
			
			$low = ($num_page - 1)*$MessageBoard_Page_Size;//从第$low条开始搜索记录
			$cmd = "select * from message_board where id_employ_message = '".$id_employMessage."' order by date_public desc LIMIT ".$low." , ".$MessageBoard_Page_Size;
			$this->sqlHelper->dbconnect();
			$result = $this->sqlHelper->execute($cmd);
			$this->sqlHelper->dbclose();
			return $result;//将搜索出来的记录返回，使用完记得free哦！
		}
		
		/*功能：展示查询出来的留言记录
		@ $id_employMessage  招聘信息的id
		@ $num_page  留言页码
		//返回：封装好的html的字符串
		//注意：本次展示由于无法完成多表选择性“left join”操作。所以只好选择效率低下的循环操作。
		//		虽然效率低下，但保证不出现数据冗余或者错误。倘有更好的方法（如是否能通过两个搜索，
		//		合并到一个$result中去，再按时间排序后输出。
		*/
		public function ShowAllByEmployMessageId($id_employMessage,$num_page)
		{
			$result = $this->SelectAllByEmployMessageId($id_employMessage,$num_page);
			if($result == null) return "";//若为空 则直接返回
			//echo '<script>alert("running!");</script>';
			$str = "";
			while($row = $this->sqlHelper->fetch_array($result))
			{
				//echo '<script>alert("running!");</script>';
				$type_role = (int)$row["type_role"];
				/*$name_role = null;
				if($type_role == 0)
				{//应聘者
					$id_role = (int)$row["id_role"];
					$cmd = "select name from graduate where id = '".$id_role."'";
					$this->sqlHelper->dbconnect();
					$name_result = $this->sqlHelper->execute($cmd);
					$this->sqlHelper->dbclose();
					$name_row = $this->sqlHelper->fetch_array($id_result);
					$this->sqlHelper->free_result($name_result);
					$name_role = $name_row["name"];
				}
				else if($type_role == 1)
				{//企业
					$id_role = (int)$row["id_role"];
					$cmd = "select companyNmae from company where id = '".$id_role."'";
					$this->sqlHelper->dbconnect();
					$name_result = $this->sqlHelper->execute($cmd);
					$this->sqlHelper->dbclose();
					$name_row = $this->sqlHelper->fetch_array($id_result);
					$this->sqlHelper->free_result($name_result);
					$name_role = $name_row["name"];
				}*/
				//直接把名字存messageBoard表里
				$str .= '
					<div class="message_m">
						<div class="message_m_logo"><img src="../images/12974102173752464b.gif" width="60" height="60" /></div>
						<div class="message_m_message">
							<span class="fc_cpn_names">'.$row["name"].'：</span>'.$row["content"].'
						</div>
						<div class="clear"></div>
					</div>
					<div class="message_re">评论时间：'.date("Y-m-d",$row["date_public"]).' | 
					<a id="id_btn_reply_'.$row["id"].'" class="class_btn_reply" href="javascript:void(0)" myOnClick="false">回复('.$row["num_reply"].')</a></div>
					<div class="message_q_con" style="display: none;" id="div_reply_all_'.$row["id"].'"></div>
				';
			}
			$this->sqlHelper->free_result($result);
			return $str;
		}
		
		/*功能：展示查询出来的留言记录
		@ $id_comment  需要展示的评论的id
		//返回：封装好的html的字符串
		*/
		public function ShowOneCommentById($id_comment)
		{
			$row = $this->SelectOneById($id_comment);
			//if($row == null) return "";//若为空 则直接返回
			//echo '<script>alert("running!");</script>';
			$str = '
				<div class="message_m">
					<div class="message_m_logo"><img src="../images/12974102173752464b.gif" width="60" height="60" /></div>
					<div class="message_m_message">
						<span class="fc_cpn_names">'.$row["name"].'：</span>'.$row["content"].'
					</div>
					<div class="clear"></div>
				</div>
				<div class="message_re">评论时间：'.date("Y-m-d",$row["date_public"]).' | 
				<a id="id_btn_reply_'.$row["id"].'" class="class_btn_reply" href="javascript:void(0)" myOnClick="false">回复('.$row["num_reply"].')</a></div>
				<div class="message_q_con" style="display: none;" id="div_reply_all_'.$row["id"].'"></div>';
			return $str;
		}
		
		/*功能：查询对应留言的回复数
		@ $id_comment  对应留言的id
		//返回：对应留言的回复数
		*/
		public function GetNumByCommentId($id_comment)
		{
			$cmd = "Select num_reply From message_board Where id = '".$id_comment."'";
			$this->sqlHelper->dbconnect();
			$result = $this->sqlHelper->execute($cmd);
			$this->sqlHelper->dbclose();
			$row = $this->sqlHelper->fetch_array($result);
			$this->sqlHelper->free_result($result);
			return (int)$row["num_reply"];
		}
		
		/*功能：获取留言数目
		@ $id_employ_message  对应招聘信息的id
		//返回：对应招聘信息的留言数目
		*/
		public function GetCommentNumByEmployMessageId($id_employ_message)
		{
			$cmd = " Select count(id) as amount From message_board Where id_employ_message = '".$id_employ_message."'";
			$this->sqlHelper->dbconnect();
			$result = $this->sqlHelper->execute($cmd);
			$this->sqlHelper->dbclose();
			$row = $this->sqlHelper->fetch_array($result);
			$this->sqlHelper->free_result($result);
			return (int)$row["amount"];
		}
		
		/*功能：分页
		@ $id_employ_message  招聘信息的id
		@ $page  当前页码
		*/
		public function ShowCommentList($id_employ_message,$page)
		{
			global $MessageBoard_Page_Size;
			$num_comment = $this->GetCommentNumByEmployMessageId($id_employ_message);
			$num_totle_page = (int)($num_comment + $MessageBoard_Page_Size - 1)/$MessageBoard_Page_Size;//获取总页数
			
			if($page<1 || $page>$num_totle_page)//如果参数页码超过
			{
				$page = 1;
			}
			
			$str = "";//最后要返回的html的String
			if($page < 12)
			{
				for($i = 0; $i<12 && $i<=$num_totle_page;$i++)
				{
				}
			}
			else
			{
				$num_low = $page - 5;
				$num_high = $page + 5;
				for($i = $num_low; $i<$num_high && $i<$num_totle_page;$i++)
				{
				}
			}
		}
		
		
		
		
	}
?>